<template>
	<view class="container p30">
		<view class="c9 tac"> {{o.type == 1?'MSD-UPDR-第一部分日常生活运动症状体验（M-EDL）':'MSD-UPDR-第一部分日常生活非运动症状体验（nM-EDL）' }}</view>
		<view class="fwb mt20">{{questNum+1}}.{{quest.content}}</view>
		<radio-group @change="radioClick">
			<label class="radio" v-for="(item,index) in quest.option" :key="index">
				<radio :value="item.score" color="#a61a32"  :data-index='index' :checked="item.checked" />
				<view>{{item.text}}</view>
			</label>
		</radio-group>
		<view class="flex flex-jcsa mt60">
			<view class="btn" v-if="questNum != 0" @click="upperQuest">上一题</view>
			<view class="btn" v-if="questNum != quests.length-1" @click="nextQuest">下一题</view>
			<view class="btn" v-else @click="submit">提交</view>
		</view>

	</view>
</template>

<script>
	import question from '../../core/question.js'

	export default {
		name: "",
		data() {
			return {
				type:0,// 1 运动， 2非运动
				quests: [], //题列表
				quest: {}, //需要答的题
				questNum: 0, //当前题数
				score: "", //分数
				isclick:true,
			}
		},
		onLoad(o) {
			this.type = o.type;
			this.quests = o.type == 1 ? this.$utils.deepClone(question.motion) : this.$utils.deepClone(question.mentality);
			this.quest = this.quests[this.questNum];
			uni.setNavigationBarTitle({
				title:o.type == 1 ? '我的运动状态自评':'我的心理状态自评'
			})
		},
		onShow() {

		},
		methods: {
			// 上一题
			upperQuest(){
				this.questNum --;
				this.quest = this.quests[this.questNum];
			},
			// 下一题
			nextQuest(){
				if(this.quest.option.every(item=>{return item.checked == false})){
					wx.showToast({
						title:"请先选择答案再下一题",
						icon:"none"
					})
					return false
				}
				this.questNum ++;
				this.quest = this.quests[this.questNum];
				this.isclick = true;
			},
			// 选择选项
			radioClick(e) {
				if(this.isclick){
					this.isclick = false;
					this.quest.option.forEach(item=>item.checked = false);
					this.quest.option[e.detail.value].checked = true;
					setTimeout(()=>{
						if(this.questNum< this.quests.length-1){
							this.nextQuest()
						}else{
							this.submit()
						}
					},100)
				}
				
			},
			// 提交答题
			submit() {
				for(let i = 0; i < this.quests.length; i++){
					if(this.quests[i].option.every(item=>{return item.checked == false})){
						wx.showToast({
							title:"请完成所有试题",
							icon:"none"
						})
						this.score = '';
						return false
					}else{
						this.score +=  String(this.quests[i].option.find(item=>{ return item.checked }).score);
					}
				}		
				this.$routeJump.redirectTo(`/plugins/selftestresult/index?score=${this.score}&type=${this.type}`)
				this.isclick = true;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container{
		background-color: #f5f5f5;
		.radio {
			display: flex;
			background-color: #fff;
			padding: 14rpx;
			margin-top: 20rpx;
			border-radius: 14rpx;
			align-items: center;
		}
		.btn{
			width: 120rpx;
			height: 76rpx;
			line-height: 76rpx;
			background-color: $uni-theme-color;
		}
	}

</style>